package org.erepublik.company.charts;

import java.util.ArrayList;
import java.util.Date;

import org.erepublik.company.CountryData;
import org.erepublik.company.EAlliance;
import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.erepublik.company.Location;
import org.erepublik.company.PriceIndex;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONCountryDataProvider;
import org.erepublik.company.service.XmlVatService;
import org.json.JSONArray;
import org.json.JSONException;

public class CountryJSONDataCollector extends AbstractJSONDataCollector<CountryData> {

    public CountryJSONDataCollector(IVatService taxService) {
        super("country", ECountryDataType.class, new XmlVatService());
    }

    @SuppressWarnings("unchecked")
    @Override
    protected double calculateOffers(CountryData[] bestOffers, Enum criteria) {
        if (criteria == ECountryDataType.POPULATION)
            return bestOffers[0].getPopulation();
        if (criteria == ECountryDataType.SPREAD)
            return bestOffers[0].getSpread();
        if (criteria == ECountryDataType.CURRENCY_PRICE)
            return bestOffers[0].getCurrencyForGold();
        if (criteria == ECountryDataType.GOLD_PRICE)
            return bestOffers[0].getGoldForCurrency();
        if (criteria == ECountryDataType.TREASURE_IN_CURRENCY)
            return bestOffers[0].getCurrencyTreasury();
        if (criteria == ECountryDataType.TREASURE_IN_GOLD)
            return bestOffers[0].getGoldTreasury();
        if (criteria == ECountryDataType.ONLINE)
            return bestOffers[0].getOnline();
        if (criteria == ECountryDataType.NEW_CITIZENS)
            return bestOffers[0].getNewCitizens();
        
        return 0;
    }

    
//    @SuppressWarnings("unchecked")
//    @Override
//    public PriceIndex[] getPricesIndex(Enum criteria, EProductType productType) {
//        PriceIndex[] priceIndex = super.getPricesIndex(criteria, productType);
//        
//        PriceIndex[] newPrices = new PriceIndex[ priceIndex.length + EAlliance.values().length ];
//        for (int i=0; i < priceIndex.length; i++) {
//            newPrices[i] = priceIndex[i];
//        }
//        
////        for (int i=0; i < EAlliance.values().length; i++) {
////            newPrices[i + priceIndex.length] = getPricesForAlliances(criteria, EAlliance.values()[i], priceIndex );
////        }
////        
//        return newPrices;
//    }
    
    
    
//    private PriceIndex getPricesForAlliances(Enum criteria, EAlliance eAlliance, PriceIndex[] priceIndex) {
//        
//        ECountryDataType countryType = (ECountryDataType) criteria;
//        int populationSum = 0;
//        double priceSum = 0;
//        
//        for (int i=0; i < priceIndex.length; i++) {
//            if (priceIndex[i].getLocation()==null)
//                continue;
//            
//            populationSum += priceIndex[i].getPopulation();
//            
//            if (countryType == ECountryDataType.CURRENCY_PRICE)
//                priceSum += priceIndex[i].getPrice() * priceIndex[i].getPopulation();
//            if (countryType == ECountryDataType.GOLD_PRICE)
//                priceSum += priceIndex[i].getPrice();
//            if (countryType == ECountryDataType.NEW_CITIZENS)
//                priceSum += priceIndex[i].getPrice();
//            if (countryType == ECountryDataType.ONLINE)
//                priceSum += priceIndex[i].getPrice();
//            if (countryType == ECountryDataType.POPULATION)
//                priceSum += priceIndex[i].getPrice();
//            if (countryType == ECountryDataType.SPREAD)
//                priceSum += priceIndex[i].getPrice() * priceIndex[i].getPopulation();           
//            if (countryType == ECountryDataType.TREASURE_IN_CURRENCY)
//                priceSum += priceIndex[i].getPrice();         
//            if (countryType == ECountryDataType.TREASURE_IN_GOLD)
//                priceSum += priceIndex[i].getPrice();         
//            
//        }
//        
//        PriceIndex alliancePriceIndex = new PriceIndex();
//        alliancePriceIndex.setLocation(eAlliance);
//        alliancePriceIndex.setPopulation(populationSum);
//        alliancePriceIndex.setPrice(priceSum);
//        
//        return alliancePriceIndex;
//    }

    
    
    @SuppressWarnings("unchecked")
    @Override
    protected CountryData[] getNBestOffers(int offers, ArrayList<CountryData> object, Enum criteria, Location country) {
        
        if (country instanceof EAlliance) {
            return new CountryData[] { getAllianceCountryData((EAlliance)country, object) };
        }
        
        for (CountryData countryData : object) {
            if (countryData.getCountry() == country )
                return new CountryData[] {countryData};
        }
        
        for (CountryData countryData : object) {
            if (countryData.getCountry() == null )
                return new CountryData[] {countryData};
        }
        throw new IllegalStateException("brak obiektu dla kraju "+country);
    }

    
    private CountryData getAllianceCountryData(EAlliance aliance, ArrayList<CountryData> object) {

        
        double averageCurrencyForGold=0;
        double totalCurrencyTreasury=0;
        double totalGoldTreasury=0;
        double averageGoldForCurrency=0;
        int totalNewCitizens=0;
        int totalOnline=0;
        int totalPopulation=0;
        

        for (CountryData countryData : object) {
            ECountry country = countryData.getCountry();
            if (country==null)
                continue;
            if (country.getAlliance() == aliance) {
                averageCurrencyForGold += countryData.getCurrencyForGold() * countryData.getPopulation();
                totalCurrencyTreasury += countryData.getCurrencyTreasury();
                totalGoldTreasury += countryData.getGoldTreasury();
                averageGoldForCurrency += countryData.getGoldForCurrency() * countryData.getPopulation();
                totalNewCitizens += countryData.getNewCitizens();
                totalOnline += countryData.getOnline();
                totalPopulation += countryData.getPopulation();
            }
        }
        averageCurrencyForGold /= totalPopulation;
        averageGoldForCurrency /= totalPopulation;
        
        CountryData allianceCountryData = new CountryData();
        allianceCountryData.setLocation(aliance);
        allianceCountryData.setCurrencyForGold(averageCurrencyForGold);
        allianceCountryData.setCurrencyTreasury(totalCurrencyTreasury);
        allianceCountryData.setGoldTreasury(totalGoldTreasury);
        allianceCountryData.setGoldForCurrency(averageGoldForCurrency);
        allianceCountryData.setNewCitizens(totalNewCitizens);
        allianceCountryData.setOnline(totalOnline);
        allianceCountryData.setPopulation(totalPopulation);
        
        
        return allianceCountryData;
    }

    
    @Override
    protected ArrayList<CountryData> getProductFromJSONArray(JSONArray jsonArrayWithFood) throws JSONException {
        ArrayList<CountryData> food = JSONCountryDataProvider.getCountryDataFromJSONArray(jsonArrayWithFood);
        return food;
    }
    
    
    public static void main(String[] args) {
        CountryJSONDataCollector collector = new CountryJSONDataCollector(new XmlVatService());
        collector.updateData();
        ChartDataSet dataSet = collector.getData(EAlliance.PHOENIX, ECountryDataType.POPULATION.toString());

        for (ChartData data : dataSet.getData()) {
            System.out.println(data.getValue());
        }
        
        dataSet = collector.getData(EAlliance.EDEN, ECountryDataType.POPULATION.toString());

        for (ChartData data : dataSet.getData()) {
            System.out.println(data.getValue() + " " + new Date(data.getTime()));
        }
//        
//        ChartDataSet dataset = dataCollector.getData(null, EFoodSearchCriteria.WELLNESS.toString());
//        List<ChartData> data = dataset.getData();

//        System.out.println( dataset.getGoogleChartFor7days() );
//        System.out.println( dataset.getGoogleChartFor24hours() );
//        System.out.println( dataset.getGoogleChartFor2months() );
    }
    
    
}
